package org.zhigong.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zhigong.dao.YonghuDao;
import org.zhigong.entity.Yonghu;
import org.zhigong.service.YonghuService;
import org.zhigong.util.CommonUtil;

import java.util.List;

import static org.zhigong.util.CommonUtil.getMD5;

@Service
public class YonghuServiceImpl implements YonghuService {

    @Autowired
    private YonghuDao yonghuDao;

    public boolean validate(Yonghu yonghu) {
        if(yonghu != null && !CommonUtil.isEmpty(yonghu.getYhm()) && !CommonUtil.isEmpty(yonghu.getMm())){
            yonghu.setMm(getMD5(yonghu.getMm()));
            List<Yonghu> list = yonghuDao.select(yonghu);
            if(list.size() == 1){
                BeanUtils.copyProperties(list.get(0),yonghu);
                return true;
            }else{
                return false;
            }
        }
        return false;
    }

    public boolean varify(String yhm) {
        return yonghuDao.varify(yhm) == 1;
    }

    public Yonghu getYonghuByYhm(String yhm) {
        return yonghuDao.queryByYhm(yhm);
    }

    public List<Yonghu> getYonghuList(Yonghu yonghu) {
        return yonghuDao.selectByPage(yonghu);
    }

    public Boolean addYonghu(Yonghu yonghu) {
        yonghu.setMm(getMD5(yonghu.getMm()));
        return yonghuDao.insertYonghu(yonghu) == 1;
    }

    public Boolean deleteYonghu(String yhm) {
        return yonghuDao.deleteYonghu(yhm) == 1;
    }

    public Boolean updateYonghu(Yonghu yonghu) {
        yonghu.setMm(getMD5(yonghu.getMm()));
        return yonghuDao.updateYonghu(yonghu) == 1;
    }
}
